package com.weiz.common;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Map;

public abstract class AbsSender {
    public abstract String getExchange();

    public abstract String getRoutingKey();

    @Autowired
    AmqpTemplate amqpTemplate;
    private void doSendMessage(Object message, Map<String, Object> headers){
        amqpTemplate.convertAndSend(getExchange(), getRoutingKey(),message, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                if (headers != null) {
                    for (String key : headers.keySet()) {
                        message.getMessageProperties().setHeader(key, headers.get(key));
                    }
                }
                return message;
            }
        });
    }
}